import {
    httpAction,
    getAction
} from '@/http/axios.js';
import E from 'wangeditor';
export const JVxeTableModelMixin = {
    data() {
        return {
            title: '操作',
            visible: false,
            confirmLoading: false,
            scrolling: true,
            ruleForm: {
            },
            uploadfileurl: this.$url.baseURL + '/upload/uploadfile',
        }
    },
    methods: {
        //上传成功后，返回图片地址
        success(response, file, fileList) {
            console.log(response);
            this.ruleForm.display = response.body.data.url
        },
        //上传拦截
        beforeUpload(file) {
            const fileName = file.name;
            const index = fileName.lastIndexOf(".");
            const ext = fileName.substr(index + 1);
            const arr = ["jpg", "png", "jpeg"];
            const isOk = arr.includes(ext);
            if (!isOk) {
                this.$message.warning(`请上传${arr.toString()}格式!`);
                return false;
            }
        },
        //初始化富文本
        setEditor() {
            this.$nextTick(res => {
                this.editor = new E(`#div2`)
                // 或者 const editor = new E( document.getElementById('div1') )
                this.editor.config.height = 200
                this.editor.config.placeholder = '请填写内容'
                this.editor.config.menus = [
                    'head',
                    'fontSize',
                    'fontName',
                    'lineHeight',
                    'foreColor',
                    'justify',
                    'image',
                ]
                this.editor.config.uploadImgShowBase64 = true
                this.editor.config.showFullScreen = false
                this.editor.config.zIndex = 5
                this.editor.config.focus = true
                this.editor.create()
            })
        },
        /** 确定按钮点击事件 */
        submitForm() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.ruleForm.cont = this.editor.txt.html()
                    this.ruleForm.token = this.$store.state.token
                    let url = this.url.add
                    if (this.ruleForm.id) {
                        url = this.url.edit
                    }
                    this.confirmLoading = true
                    httpAction(url, this.ruleForm).then(res => {
                        console.log(res);
                        if (res.state.code === '0') {
                            this.$emit('ok')
                            this.$message.success(res.state.message)
                        } else {
                            this.$message.error(res.state.message)
                        }
                    }).finally(() => {
                        this.confirmLoading = false
                    })
                } else {
                    return false;
                }
            });
        },
        //清空数据
        resetForm() {
            this.$refs['ruleForm'].resetFields();
        },
        /** 当点击了编辑（修改）按钮时调用此方法 */
        edit(record) {
            this.visible = true
            this.ruleForm = Object.assign({}, record)
            this.setEditor()
            this.$nextTick(res => {
                this.editor.txt.html(record.cont);
            })
        },

        /** 关闭按钮点击事件 */
        handleCancel() {
            this.visible = false
            this.$emit('close')
        },

    }
}
